<?php
// PHP Proxy example for Yahoo! Web services.
// Responds to both HTTP GET and POST requests
//
// Author: Jason Levitt
// December 7th, 2005
//
// Update: Custimize proxy to use with flickr rest requests only
// By: Fabrizio Angius, July 24th, 2008

// TODO: add more security checks if possible to avoid abuse.

// Allowed hostname (api.local and api.travel are also possible here)
define ('HOSTNAME', 'http://api.flickr.com/services/rest/');


function error() {
	// TODO: this is temporary
	$url = 'http://'.$_SERVER['HTTP_HOST'].'/abuse.php';
	header('Location: '.$url);
	header('HTTP/1.0 301 Permanent Redirect');
	header('Status: 301 Permanent Redirect');
}

// Get the REST call path from the AJAX application
// Is it a POST or a GET?
$api = $_POST['api_key'];
if (strlen($api) != 32) {
	error();
	return;
}

$url = HOSTNAME.$path;

// Open the Curl session
$session = curl_init($url);

$postvars = '';
while ($element = current($_POST)) {
	$postvars .= key($_POST).'='.$element.'&';
	next($_POST);
}
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);


// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// Make the call
$data = curl_exec($session);

// The web service returns XML. Set the Content-Type appropriately
//header("Content-Type: text/xml");
header("Content-Type: application/json");

// Clean result by removing _content nodes
$data = preg_replace("/:{\"_content\":(.*?)}/", ":\\1", $data);

echo '(';
echo $data;
echo ')';
curl_close($session);

?>
